//repository
var STB_Repo = function(){
    var that = this;
    this.dao = null;
    this.queryTemplates = null;
    this.bill = null;
    this.init = function(){
        this.setQueryTemplates();
        this.dao = new STB_persist();
        this.items = new Array();
        this.buyers = new Array();
    };    
//bill functions
    this.findBill = function(ident, isLocal, successFunc, errorFunc){
        var queryString = this.queryTemplates.findBillLocal;
        if(isLocal == false){
            queryString = this.queryTemplates.findBillDb;
        }
        this.dao.queryDb(
            queryString, 
            [ident], 
            function(args, result){
                if(result.rows.length > 0){
                    var billData = result.rows.item(0);
                    var Bill = new STB_bill();
                    Bill.mapToAttribs(billData);
                    successFunc.call(this, Bill);  
                }
            }, 
            errorFunc
        );  
    };
    
    this.findCurrentBill = function(successFunc, errorFunc){
        this.dao.queryDb(
            this.queryTemplates.currentBill, 
            [], 
            function(args, result){
                if(result.rows.length > 0){
                    var billData = result.rows.item(0);
                    var currBill = new STB_bill();
                    currBill.mapToAttribs(billData);
                    successFunc.call(this, currBill);  
                }
            }, 
            function(args, result){
                debugger;
            }
        ); 
    };    
    
    this.makeNewBill = function(name, successFunc, errorFunc){
        var newBill = new STB_bill();
        newBill.setName(name);
        this.dao.queryDb(
            this.queryTemplates.newBill, 
            [
                newBill.getIdent(), 
                newBill.getName(), 
                newBill.getDateCreated()
            ], 
            function(args, result){
                newBill.setDbIdent(result.insertId);
                successFunc.call(this,newBill);  
            }, 
            errorFunc
        );        
    };
    
    this.getCurrentBill = function(){
        return this.bill;
    };
    
    this.setCurrentBill = function(Bill){
        this.bill = Bill;
    };
    
    this.getBillsWithLimit = function(limit, callBack, errorFunc){
        this.dao.queryDb(    
            this.queryTemplates.allBillsWithLimit, 
            [limit], 
            function(args, result){
                var billsList = [];
                if(result.rows.length > 0){
                    for(var i=0;i<result.rows.length;i++){
                        var billData = result.rows.item(i);
                        var billObj = new STB_bill();
                        
                        billObj.mapToAttribs(billData);
                        billsList.push(billObj);
                    }
                }
                
                callBack.call(this, billsList);  
            }, 
            errorFunc
        );
    };
    
    this.getBills = function(callBack, errorFunc){
        this.dao.queryDb(    
            this.queryTemplates.allBills, 
            [], 
            function(args, result){
                var billsList = [];
                if(result.rows.length > 0){
                    for(var i=0;i<result.rows.length;i++){
                        var billData = result.rows.item(i);
                        var billObj = new STB_bill();
                        
                        billObj.mapToAttribs(billData);
                        billsList.push(billObj);
                    }
                }
                
                callBack.call(this, billsList);  
            }, 
            errorFunc
        );
    };
    
//buyer functions
    this.addPerson = function(name, currBillId, successFunc, errorFunc){
        if(typeof name != 'undefined' && name != ''){
            var newPerson = new STB_buyer();
            newPerson.setName(name);
            newPerson.setBillId(currBillId);
            this.dao.queryDb(
                this.queryTemplates.newPerson, 
                [
                    newPerson.getBillId(),
                    newPerson.getIdent(), 
                    newPerson.getName(), 
                    newPerson.getDateCreated()
                ], 
                function(args, result){
                    newPerson.setDbIdent(result.insertId);
                    successFunc.call(newPerson);  
                }, 
                errorFunc
            );        
        }
    };    
    
    this.getPeople = function(billId, callBack, errorFunc){
        this.dao.queryDb(
            this.queryTemplates.getPeople, 
            [
                billId
            ], 
            function(args, result){
                var personsList = new Array;
                if(result.rows.length > 0){
                    for(var i=0;i<result.rows.length;i++){
                        var personData = result.rows.item(i);
                        var personObj = new STB_buyer();
                        personObj.mapToAttribs(personData);
                        personsList.push(personObj);
                    }
                }
                callBack.call(this, personsList);  
            }, 
            errorFunc
        );        
    };
    
    this.getBuyers = function(){
        return this.bill.getBuyers();
    };
    
    this.getBuyer = function(ident, search){
        return this.bill.getBuyer(ident);
    };
    
    this.addBuyer = function(name){
        var buyer = new STB_buyer();
        buyer.setName(name);    
        //this.buyers.push(buyer);
        
        this.bill.addBuyer(buyer);
    };
    
    this.removeBuyer = function(ident){
        this.bill.removeBuyer(ident);
    };
//item functions
    this.getItems = function(){
         return this.bill.getItems();       
    };
        
    this.getItem = function(ident, search){
        return this.bill.getItem(ident);
    };
    
    this.addItem = function(name, billId,price, qty, successFunc, errorFunc){
        var item = new STB_item();
        item.setName(name);
        item.setPrice(price);
        item.setBillId(billId);
        item.setQty(qty);
        this.bill.addItem(item);
        //id_local_ite, id_bil, name_ite, qty_ite, price_ite, date_create_ite
        this.dao.queryDb(
            this.queryTemplates.newItem, 
            [
                item.getIdent(), 
                item.getBillId(),
                item.getName(), 
                item.getQty(),
                item.getPrice(),
                item.getDateCreated()
            ], 
            function(args, result){
                item.setDbIdent(result.insertId);
                successFunc.call(this, item);  
            }, 
            errorFunc
        );        
    };
    
    this.removeItem = function(ident){
        this.bill.removeItem(ident);        
    };
// persistence functions
    this.persistAll = function(){
        
    };
    
    this.populate = function(){
        
    };
    
    this.persist = function(){
        
    };
    
    this.setQueryTemplates = function(){
        this.queryTemplates = {
            //bill queries
            newBill: 'INSERT INTO stb_bill_bil(id_local_bil, name_bil, date_create_bil) VALUES(?,?,?)',
            currentBill: 'SELECT id_bil, id_local_bil, name_bil, date_create_bil FROM stb_bill_bil ORDER BY create_stamp_bil desc LIMIT 1',        
            allBills: 'SELECT id_bil, id_local_bil, name_bil, date_create_bil FROM stb_bill_bil ORDER BY create_stamp_bil desc',
            allBillsWithLimit: 'SELECT id_bil, id_local_bil, name_bil, date_create_bil FROM stb_bill_bil ORDER BY create_stamp_bil desc LIMIT ?',
            newPerson: 'INSERT INTO stb_person_per(id_bil, id_local_per, name_per, date_create_per) VALUES(?,?,?,?)',
            getPeople: 'SELECT id_per, id_local_per, id_bil, name_per, date_create_per FROM stb_person_per WHERE id_bil=?',
            findBillLocal: 'SELECT id_bil, id_local_bil, name_bil, date_create_bil FROM stb_bill_bil WHERE id_local_bil=?',
            findBillDb: 'SELECT id_bil, id_local_bil, name_bil, date_create_bil FROM stb_bill_bil WHERE id_bil=?',
            newItem: 'INSERT INTO stb_item_ite(id_local_ite, id_bil, name_ite, qty_ite, price_ite, date_create_ite) VALUES(?,?,?,?,?,?)'
        };
    };  
    
    this.init();
};

